package com.wpw.web.base;

/**
 * @Description:
 * @ClassName: ThreadLocalTest
 * @CreateDate: 2020-06-13 11:09
 * @author: wupengwei
 */
public class ThreadLocalTest {

   // static ThreadLocal<String> tr = new ThreadLocal<>();

    public static void main(String[] args) {

        ThreadLocal<String> tr = new ThreadLocal<>();
        for (int i = 1; i <= 10; i++) {
            Thread thread = new Thread(() -> {
                tr.set(Thread.currentThread().getName());
                testValue(tr);
            });

            thread.setName("threadLocal" + i);
            thread.start();
        }

        ThreadLocal<String> stringThreadLocal = ThreadLocal.withInitial(() -> new String(System.currentTimeMillis() + ""));
       // stringThreadLocal.set("dd");
        System.out.println(stringThreadLocal.get());

    }

    public static void testValue(ThreadLocal<String> tr) {
        String name = Thread.currentThread().getName();
        String s = tr.get();
        System.out.println(name + "===" + s);
    }
}